class PhongsController < ApplicationController
  # GET /phongs
  # GET /phongs.json
  def index
    @per_page = Param.get_value("itemsperpage")
    @search = Phong.where('MaPhong LIKE ? OR TenPhongg LIKE ? OR MaLoai_id LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%")
    @phongs = @search.paginate(:page => params[:page], :per_page => @per_page)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @phongs }
      format.csv { send_data Phong.to_csv(@search) }
      format.xls
    end
  end

  # GET /phongs/1
  # GET /phongs/1.json
  def show
    @phong = Phong.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @phong }
    end
  end

  # GET /phongs/new
  # GET /phongs/new.json
  def new
    if sign_in?
      @phong = Phong.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @phong }
      end
    else
      render 'sessions/deny'
    end
  end

  # GET /phongs/1/edit
  def edit
    if sign_in?
      @phong = Phong.find(params[:id])
    else
      render 'sessions/deny'
    end
  end

  # POST /phongs
  # POST /phongs.json
  def create
    @phong = Phong.new(params[:phong])

    respond_to do |format|
      if @phong.save
        format.html { redirect_to @phong, notice: 'Phong was successfully created.' }
        format.json { render json: @phong, status: :created, location: @phong }
      else
        format.html { render action: "new" }
        format.json { render json: @phong.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /phongs/1
  # PUT /phongs/1.json
  def update
    @phong = Phong.find(params[:id])

    respond_to do |format|
      if @phong.update_attributes(params[:phong])
        format.html { redirect_to @phong, notice: 'Phong was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @phong.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /phongs/1
  # DELETE /phongs/1.json
  def destroy
    @phong = Phong.find(params[:id])
    @phong.destroy

    respond_to do |format|
      format.html { redirect_to phongs_url }
      format.json { head :no_content }
    end
  end

  def import
    if sign_in?
      @ts_import = Phong.new
      respond_to do |format|
        format.html
      end
    else
      render 'sessions/deny'
    end
  end

  # Tạo đường dẫn để import
  def save
    if (Phong.import(params[:file]))
      redirect_to import_phongs_path, notice: t("flash.import_msg")
    else
      redirect_to import_phongs_path, notice: t("flash.import_error")
    end
  end

  def getCtPhong
    @ctp = CtPhong.getAllbyPhong(params[:phongid],nil,nil)
    respond_to do |format|
      format.json {render json: @ctp}
    end
  end

  def getSLTS
    @sl = CtPhong.getSLTaiSan(params[:phongid],params[:tsid])
    respond_to do |format|
      format.json {render json: @sl}
    end
  end
  def addTS
    if (CtPhong.addTaiSan(params[:pid],params[:tsid],params[:soluong]))
      redirect_to import_phongs_path, notice: t("flash.import_msg")
    else
      redirect_to import_phongs_path, notice: t("flash.import_error")
    end
  end
  def suaTS
    if (CtPhong.ThayDoiTT(params[:pid],params[:tsid],params[:tt1],params[:tt2],params[:soluong]))
      redirect_to import_phongs_path, notice: t("flash.import_msg")
    else
      redirect_to import_phongs_path, notice: t("flash.import_error")
    end
  end
  def chuyenTS
    if (CtPhong.ChuyenTS(params[:tsid],params[:p1id],params[:p2id],params[:soluong]))
      redirect_to import_phongs_path, notice: t("flash.import_msg")
    else
      redirect_to import_phongs_path, notice: t("flash.import_error")
    end
  end
end
